package com.laolei.core.doc.config;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author lei
 * @version 1.0.0
 */

@Configuration
@ComponentScan
public class Swagger2Config {
	
	@Profile({"prod","uat"}) @RestController static class PreventSwaggerResourcesController { @RequestMapping("/swagger-ui.html") void swaggerIndex() {} @RequestMapping("/webjars/springfox-swagger-ui/**") void swaggerResources() {} }

	@EnableConfigurationProperties(AppConfig.class)
    @Profile({"dev","test"}) @EnableSwagger2 static class Swagger2Docket extends Docket implements InitializingBean{
		 @Autowired AppConfig appConfig;
		public Swagger2Docket() {
			super(DocumentationType.SWAGGER_2);
		}
		@Override
		public void afterPropertiesSet() throws Exception {
			this.apiInfo(new ApiInfoBuilder()
                    .title(appConfig.getName()+"的api接口")
                    .description(appConfig.getDescription())
                    .termsOfServiceUrl(" ")
                    .version(appConfig.getVersion())
                    .build())
            .groupName(appConfig.getName()+"的api接口文档")
            .pathMapping(appConfig.getPath())//最终调用接口后会和paths拼接在一起
			.select()
			.apis(RequestHandlerSelectors.basePackage(appConfig.getBasePackage()))
			.paths(PathSelectors.any())
			.build();
		}}
}
